package com.bell.atensettings.settingDetails;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;

import softwinner.os.ChipInfo;

import com.bell.atensettings.R;

import android.app.Activity;
import android.app.ActivityManager;
import android.app.ActivityManager.MemoryInfo;
import android.content.Context;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.os.StatFs;
import android.text.format.Formatter;
import android.util.Log;
import android.widget.TextView;

public class DeviceInfoSettings extends Activity
{
    private TextView tv_deviceType;
    private TextView tv_ramSize;
    private TextView tv_diskSize;
    private TextView tv_serial;

    private String getAvaliableSize() {
        ActivityManager am = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
        MemoryInfo mi = new MemoryInfo();

        am.getMemoryInfo(mi);

        return Formatter.formatFileSize(getBaseContext(), mi.availMem);// 将获取的内存大小规格化
    }

    /**
     * 获取内存大小
     * 
     * @return
     */
    private String getTotalMemory() {
        String meminfo = "/proc/meminfo";// 系统内存信息文件
        String str2;
        String[] memInfo;
        long initial_memory = 0;
        try {
            BufferedReader reader = new BufferedReader(new FileReader(meminfo), 8192);
            str2 = reader.readLine();// 读取meminfo第一行，系统总内存大小

            memInfo = str2.split("\\s+");
            for (String num : memInfo) {
                Log.i(str2, num + "\t");
            }

            initial_memory = Integer.valueOf(memInfo[1]).intValue() * 1024;// 获得系统总内存，单位是KB，乘以1024转换为Byte
            reader.close();

        } catch (IOException e) {
        }
        return Formatter.formatFileSize(getBaseContext(), initial_memory);// Byte转换为KB或者MB，内存大小规格化
    }

    private String getTotalSize() {
        ActivityManager am = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
        MemoryInfo mi = new MemoryInfo();

        am.getMemoryInfo(mi);

        return Formatter.formatFileSize(getBaseContext(), mi.availMem);// 将获取的内存大小规格化
    }

    public long[] getSDCardMemory() {
        long[] sdCardInfo = new long[2];
        String state = Environment.getExternalStorageState();
        if (Environment.MEDIA_MOUNTED.equals(state)) {
            File sdcardDir = Environment.getExternalStorageDirectory();
            StatFs sf = new StatFs(sdcardDir.getPath());
            long bSize = sf.getBlockSize();
            long bCount = sf.getBlockCount();
            long availBlocks = sf.getAvailableBlocks();

            sdCardInfo[0] = bSize * bCount;// 总大小
            sdCardInfo[1] = bSize * availBlocks;// 可用大小
        }
        return sdCardInfo;
    }

    public long[] getRomMemroy() {
        long[] romInfo = new long[2];
        // Total rom memory
        romInfo[0] = getTotalInternalMemorySize();

        // Available rom memory
        File path = Environment.getDataDirectory();
        StatFs stat = new StatFs(path.getPath());
        long blockSize = stat.getBlockSize();
        long availableBlocks = stat.getAvailableBlocks();
        romInfo[1] = blockSize * availableBlocks;
        return romInfo;
    }

    public long getTotalInternalMemorySize() {
        File path = Environment.getDataDirectory();
        StatFs stat = new StatFs(path.getPath());
        long blockSize = stat.getBlockSize();
        long totalBlocks = stat.getBlockCount();
        return totalBlocks * blockSize;
    }

    private void initView() {
        tv_deviceType = (TextView) findViewById(R.id.device_type);
        tv_ramSize = (TextView) findViewById(R.id.device_ramsize);
        tv_diskSize = (TextView) findViewById(R.id.device_disksize);
        tv_serial = (TextView) findViewById(R.id.device_serial);

        Log.e("DeviceInfo", Formatter.formatFileSize(getBaseContext(), getRomMemroy()[0]));
        Log.e("DeviceInfo", Formatter.formatFileSize(getBaseContext(), getSDCardMemory()[0]));
        tv_deviceType.setText(Build.MODEL);
        tv_ramSize.setText(getTotalMemory() + "/" + getAvaliableSize() + "可用");
        tv_diskSize.setText(Formatter.formatFileSize(getBaseContext(), getSDCardMemory()[0]) + "/"
                + Formatter.formatFileSize(getBaseContext(), getSDCardMemory()[1]) + "可用");

        Log.e("DeviceInfo", ChipInfo.getChipIDHex());
        tv_serial.setText(ChipInfo.getChipIDHex());
    }

    @Override
    protected void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        setContentView(R.layout.device_info);

        initView();
    }
    
    @Override
    public void onBackPressed()
    {
        super.onBackPressed();
        overridePendingTransition(R.anim.start, R.anim.push_left_out);
    }

}
